package com.fuhe.chen.vendingmachine.controller.admin;

import com.alipay.api.response.AlipayTradeRefundResponse;
import com.fuhe.chen.vendingmachine.alipay.Alipay;
import com.fuhe.chen.vendingmachine.anno.AddLog;
import com.fuhe.chen.vendingmachine.common.Utils;
import com.fuhe.chen.vendingmachine.constant.WebConst;
import com.fuhe.chen.vendingmachine.dto.ApiResponse;
import com.fuhe.chen.vendingmachine.dto.ApiResponseForList;
import com.fuhe.chen.vendingmachine.constant.MessageConstant;
import com.fuhe.chen.vendingmachine.dto.cond.BookOrderCond;
import com.fuhe.chen.vendingmachine.pojo.BookOrder;
import com.fuhe.chen.vendingmachine.pojo.RefundApply;
import com.fuhe.chen.vendingmachine.pojo.User;
import com.fuhe.chen.vendingmachine.service.IBookOrderService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin/bookOrderAdmin")
@ResponseBody
@Api(tags = "预约订单管理")
public class BookOrderAdminController {

    private static final Logger LOGGER = LoggerFactory.getLogger(BookOrderAdminController.class);

    @Autowired
    IBookOrderService bookOrderService;

    @ApiOperation(value = "查询用户订单列表", notes = "参数:用户id,订单状态,页数")
    @PostMapping("/getBookOrderList")
    public ApiResponseForList<BookOrder> getBookOrderList(@RequestParam(name = "machineId", required = false) Integer machineId,
                                                          @RequestParam(name = "status", required = false) Integer status,
                                                          @RequestParam(name = "orderId", required = false) String orderId,
                                                          @RequestParam(name = "user_id", required = false) String user_id,
                                                          @RequestParam(name = "payMethod", required = false) Integer payMethod,
                                                          @RequestParam(name = "create_date_start", required = false) Long create_date_start,
                                                          @RequestParam(name = "create_date_end", required = false) Long create_date_end,
                                                          @RequestParam(name = "pay_date_start", required = false) Long pay_date_start,
                                                          @RequestParam(name = "pay_date_end", required = false) Long pay_date_end,
                                                          @RequestParam("pageNum") Integer pageNum,
                                                          HttpServletRequest request
    ) {
        BookOrderCond cond = new BookOrderCond();
        cond.setUser_id(user_id);
        cond.setMachineId(machineId);
        cond.setOrderId(orderId);
        cond.setPayMethod(payMethod);
        cond.setStatus(status);
        cond.setCreate_date_start(create_date_start);
        cond.setCreate_date_end(create_date_end);
        cond.setPay_date_start(pay_date_start);
        cond.setPay_date_end(pay_date_end);

        ApiResponseForList<BookOrder> response = new ApiResponseForList<>();
        PageInfo<BookOrder> bookOrderPageInfo = bookOrderService.queryOrderByCondition(cond, pageNum, 10);
        response.setData(bookOrderPageInfo.getList());
        response.setSuccess(true);
        response.setCode(MessageConstant.Success.SUCCESS_CODE);
        response.setMessage(MessageConstant.Success.QUERY_SUCCESS_MESSAGE);

        return response;
    }

    @ApiOperation(value = "查询订单详情", notes = "参数:订单id")
    @PostMapping("/getOrderDetail")
    public ApiResponse<BookOrder> getOrderDetail(@RequestParam("order_id") String order_id) {

        BookOrder bookOrder = bookOrderService.queryOrderByOrderIdForAdmin(order_id);

        ApiResponse<BookOrder> response = new ApiResponse<>();
        response.setSuccess(true);
        response.setCode(MessageConstant.Success.SUCCESS_CODE);
        response.setMessage(MessageConstant.Success.QUERY_SUCCESS_MESSAGE);
        response.setData(bookOrder);
        return response;
    }

    @ApiOperation(value = "查询退款订单申请列表")
    @GetMapping("/getBookOrderRefundApply")
    public ApiResponse<PageInfo<RefundApply>> getBookOrderRefundApply(@RequestParam("pageNum") Integer pageNum,
                                                                      @RequestParam("pageSize") Integer pageSize) {

        PageInfo<RefundApply> refundApply = bookOrderService.queryBookOrderRefundApply(pageNum, pageSize);

        ApiResponse<PageInfo<RefundApply>> response = new ApiResponse<>();
        response.setSuccess(true);
        response.setCode(MessageConstant.Success.SUCCESS_CODE);
        response.setMessage(MessageConstant.Success.QUERY_SUCCESS_MESSAGE);
        response.setData(refundApply);
        return response;
    }

    @ApiOperation(value = "同意退款申请")
    @PostMapping("/approveRefund")
    public ApiResponse<String> approveRefund(
            @RequestParam("bookOrderId") String bookOrderId,
            @RequestParam("refund_reason") String refund_reason,
            @RequestParam("refundApplyId") Integer refundApplyId) {
        BookOrder bookOrder = bookOrderService.queryOrderByOrderId(bookOrderId);
        AlipayTradeRefundResponse refundResult = Alipay.refund(bookOrderId, String.format("%.2f", bookOrder.getAmount()), refund_reason);

        ApiResponse<String> response = new ApiResponse<>();
        response.setCode(MessageConstant.Success.SUCCESS_CODE);
        response.setMessage(MessageConstant.Success.DO_SUCCESS_MESSAGE);
        //退款成功 修改订单状态
        if (refundResult.getFundChange().equals("Y")) {
            bookOrderService.approveRefund(bookOrderId, refundApplyId);
            response.setSuccess(true);
            response.setCode(10000);
            response.setMessage("退款成功,退款路径:支付宝");
        } else {
            response.setSuccess(false);
            response.setCode(54444);
            response.setMessage("支付宝退款失败");
        }
        return response;
    }

    @ApiOperation(value = "拒绝退款申请")
    @PostMapping("/refuseRefund")
    public ApiResponse<String> refuseRefund(
            @RequestParam("bookOrderId") String bookOrderId,
            @RequestParam("refundApplyId") Integer refundApplyId) {
        ApiResponse<String> response = new ApiResponse<>();
        String result = bookOrderService.refuseRefund(bookOrderId, refundApplyId);
        if(result.equals(MessageConstant.Success.DO_SUCCESS_MESSAGE)){
            response.setSuccess(true);
            response.setCode(MessageConstant.Success.SUCCESS_CODE);
            response.setMessage(result);

        }else {
            response.setSuccess(false);
            response.setCode(MessageConstant.Admin.REFUSE_REFUND_FAILURE_CODE);
            response.setMessage(MessageConstant.Admin.REFUSE_REFUND_FAILURE_MESSAGE);
            response.setData(result);
        }
        return response;
    }
}
